﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace EpubFactory.Common
{
    public class Book
    {
        public string Title { get; set; }
        public List<string> Authors { get; set; }
        public string CoverUrl { get; set; }
        public List<Chapter> Chapters { get; set; }
        private IEnumerable<Chapter> FlatTopDownChapters(IEnumerable<Chapter> chapters)
        {
            if (chapters != null)
            {
                foreach (var chapter in chapters)
                {
                    yield return chapter;
                    foreach (var item in FlatTopDownChapters(chapter.Children))
                    {
                        yield return item;
                    }
                }
            }
        }
        private IEnumerable<Chapter> FlatBottomUpChapters(IEnumerable<Chapter> chapters)
        {
            if (chapters != null)
            {
                foreach (var chapter in chapters)
                {
                    foreach (var item in FlatBottomUpChapters(chapter.Children))
                    {
                        yield return item;
                    }
                    yield return chapter;
                }
            }
        }
        public IEnumerable<Chapter> FlattedChapters => FlatTopDownChapters(this.Chapters);
        public string Summary { get; set; }
        public string Subject { get; set; }
        public string EPubId { get; set; }
        public string Host { get; set; }
        public Book()
        {
            Authors = new List<string>();
            Subject = "Default";
            //Chapters = new List<Chapter>();
        }
        public void GenerateChaptersId()
        {
            var max = Chapters.Last().SortOrder.ToString().Length;

            foreach (var chapter in Chapters)
            {
                chapter.Id = chapter.SortOrder.ToString().PadLeft(max, '0');

                if (chapter.Children != null)
                {
                    foreach (var child in chapter.Children)
                    {
                        child.Id = child.SortOrder.ToString().PadLeft(max, '0');
                    }
                }
            }
        }
        public List<string> Images { get; set; }
    }
    public class Chapter
    {
        public string Title { get; set; }
        public int SortOrder { get; set; }
        public string Id
        {
            get;
            set;
        }
        public string FileName { get { return Path.GetFileName(this.FilePath); } }
        public string FilePath { get; set; }
        public string Href { get; set; }
        [XmlIgnore]
        public string SourceContent { get; set; }
        [XmlIgnoreAttribute]
        public Chapter Parent { get; set; }
        public string Content { get; set; }

        public List<Chapter> Children { get; set; }

        [XmlIgnoreAttribute]
        public Book Book { get; set; }
    }
    public class Entry
    {
        public string LocalPath { get; set; }
        public string Uri { get; set; }
        public string FileName { get; set; }
    }

    public interface IGenerator
    {
        void Generate(Book book, string target);
    }
    public class TxtGenerator : IGenerator
    {
        public void Generate(Book book, string target)
        {
            throw new NotImplementedException();
        }
    }
    public abstract class EpubGenerator : IGenerator
    {
        public abstract void Generate(Book book, string target);
    }
    public class EpubV201Generator : EpubGenerator
    {
        public override void Generate(Book book, string target)
        {
            throw new NotImplementedException();
        }
    }
    public class MobiGenerator : IGenerator
    {
        public void Generate(Book book, string target)
        {
            throw new NotImplementedException();
        }
    }
}
